﻿@{
    ViewBag.Title = "DeliveryIn";
}
@section scripts
{   
    <script src="@Url.Content("~/Scripts/jquery-ui-1.11.4.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js?ver=GetDate()")" type="text/javascript"></script>  
    <script src="@Url.Content("~/Scripts/Bill/Bill.js?ver=GetDate()")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Bill/scan.js?ver=GetDate()")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/zxx.drag.1.0.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/Print.js?ver=GetDate()")" type="text/javascript"></script>
    <link href="~/Scripts/miniui/themes/default/medium-mode.css" rel="stylesheet" type="text/css" />       
    <style type="text/css">
        .window-mask {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                filter: alpha(opacity=40);
                opacity: 0.40;
                font-size: 1px;
                overflow: hidden;
                display:none;
                background-color: gray;
            }
    </style>
}
<body>
    <form id="form1" runat="server" method="post" style="height: 100%">

        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;">
            <table width="1000px;">
                <tr>
                    <td width="1000px;">
                        <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="newBills" plain="true" visible="@ViewBag.save">新增</a>
                        <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveBill" plain="true" visible="@ViewBag.save">保存</a>                        
                        <a class="mini-button" id="ScanAdd" name="ScanAdd" iconCls="icon-find" onclick="ScanAdd" plain="true" visible="@ViewBag.save">扫描</a>
                        <a class="mini-button" id="addrow" name="addrow" iconCls="icon-addnew" onclick="addRow" plain="true" visible="@ViewBag.save">增行</a>
                        <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRow" plain="true" visible="@ViewBag.save">删行</a>                        
                        <a class="mini-button" id="modify" name="modify" iconCls="icon-edit" onclick="modify" plain="true" visible="@ViewBag.save">改单</a>
                        <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="checkBill" plain="true" visible="@ViewBag.check">审核</a>                        
                        <a class="mini-menubutton" plain="true" iconCls="icon-expand" menu="#popupMenu">选项</a>
                        <a class="mini-button" id="print" iconCls="icon-print" onclick="printBillMyseft" plain="true" visible="@ViewBag.print">打印</a>
                        <a class="mini-button" id="getdata" iconCls="icon-collapse" onclick="getBillData" plain="true">浏览</a>
                        <span class="separator"></span>
                        <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindowBill" plain="true">关闭</a>
                    </td>
                </tr>
            </table>
        </div>

        <div id="DivHeader">
            <table style="width:770px;">
                <tr>
                    <td width="80px" align="right">配送门店：</td>
                    <td width="190px">
                        @Html.Partial("~/Views/Delivery/LookupStockRelevant.cshtml")
                    </td>
                    <td width="80px" align="right">开单日期：</td>
                    <td width="190px">
                        <input name="FDate" class="mini-datepicker" style="width:190px;" />
                    </td>
                    <td width="80px" align="right">单据编号：</td>
                    <td width="150px">
                        <input name="FBillNo" class="mini-textbox" required="true" readonly="false" style="width:150px;" />
                    </td>
                </tr>
                <tr>
                    <td align="right">收货门店：</td>
                    <td>                        
                        @Html.Partial("~/Views/Modular/LookupStockInfo.cshtml")
                    </td>
                    <td align="right">单据备注：</td>
                    <td>
                        <input name="FRemark" class="mini-textbox" style="width:190px;" />
                    </td>
                    <td align="right">单据状态：</td>
                    <td>
                        <input name="txtCheckFlag" class="mini-textbox" readonly="readonly" style="width:150px;" />
                    </td>
                </tr>
                <tr>
                    <td align="right">配送单号：</td>
                    <td>
                        <input id="btnFBillNO_Dawout" name="FBillNO_Dawout" class="mini-buttonedit" style="width:199px;" onbuttonclick="onGetSourceBill" />
                    </td>
                    <td align="right"></td>
                    <td>
                       
                    </td>
                    <td align="right"></td>
                    <td>
                    </td>
                </tr>
            </table>
        </div>
        <div class="mini-fit">
            @Html.Partial("~/Views/Modular/Bill/DataGridDawIn.cshtml")
        </div>

        @Html.Partial("~/Views/Modular/Bill/Footer.cshtml")
        <input name="KInterID_DawOut" class="mini-hidden" />
        <input name="KInterID_Pra" class="mini-hidden" />
    </form>
    @Html.Partial("~/Views/Modular/Bill/ScanWin.cshtml")

    <input id="type" name="type" value="@ViewBag.type" type="hidden"/>
    <input id="billid" name="type" value="@ViewBag.billid" type="hidden"/>

        <!--menu-->
    <ul id="popupMenu" class="mini-menu" style="display:none;">    
        <li id ="import" name="import" iconCls="icon-redo" onclick="ExcelImport">从Excel文件导入</li>          
	    <li id ="ScanAdd" name="ScanAdd" iconCls="icon-find" onclick="ScanAdd" visible="@ViewBag.save">扫描录入</li>         
        <li id ="uncheck" name="uncheck" iconCls="icon-unlock" onclick="checkBillUn" visible="@ViewBag.uncheck">反审核</li>
    </ul>

    <script type="text/javascript">
        mini.parse();
        var form = new mini.Form("form1");
        var grid = mini.get("datagrid1");      
        var uID = parent.window.getSystemAppID();
        var uCode = parent.window.getSystemAppCode();
        var uName = parent.window.getSystemAppName();

        $(function () {
            var oBox = document.getElementById("scan");
            var oBar = document.getElementById("title");
            InitScan();
            if (id == 0) BillState(0);
            startDrag(oBar, oBox);
            InitGrid("DeliveryIn");
        });
        var id = getQueryString("billid");
        if (id != null) {
            mini.getbyName("KInterID").setValue(id);
        }        
        var datatype = getQueryString("datatype");      
        $(function () {
            LoadData();
        });

        function LoadData() {
            var minterid = mini.getbyName("KInterID").value;
            $.ajax({
                url: '../Bill/GetBillData',
                data: { mSystemAppID: uID, mBillID: minterid, mDataType: datatype },
                dataType: 'text',
                async: false,
                success: function (text) {
                    var dataR = mini.decode(text);
                    debugger;
                    grid.setData(dataR.datadetail);
                    if (dataR.datamaster.length > 0) {
                        var mdata = dataR.datamaster[0];
                        if (mdata.FDataType == 11 || mdata.FDataType == 12)
                            form.setData(mdata);
                        mini.getbyName("FStockID").setValue(mdata.FStockID);
                        mini.getbyName("FStockID").setText(mdata.FStockNumber);
                        mini.getbyName("FStockName").setValue(mdata.FStockName);
                        mini.getbyName("FRStockID").setValue(mdata.FRStockID);
                        mini.getbyName("FRStockID").setText(mdata.FRStockNumber);
                        mini.getbyName("FRStockName").setValue(mdata.FRStockName);
                        mini.getbyName("FBillNO_Dawout").setValue(mdata.KInterID_DawOut);
                        mini.getbyName("FBillNO_Dawout").setText(mdata.mFBillNo_DawOut);
                        mini.getbyName("ScanAdd").setEnabled(false);
                        if (mdata.FCheckFlag == 1) {
                            BillState(2);
                        }
                        else {
                            BillState(1);
                        }
                        if ((mdata.KInterID == "0") || (mdata.KInterID == null)) {
                            newBills();
                        }
                    }
                    else {
                        newBills();
                    }
                }
            });
        }

        function newBills() {
            NewBill();
            var date = new Date();
            mini.getbyName("FBillDate").setValue(date);
            mini.getbyName("FBillNO_Dawout").setValue("");
            mini.getbyName("FBillNO_Dawout").setText("");
        }
        function checkValid() {
            if ((mini.getbyName("FBillerID").getValue() == "") || (mini.getbyName("FBillerID").getValue() == "0")) {
                var newbillerid = parent.window.getSystemAppID();
                mini.getbyName("FBillerID").setValue(newbillerid);
            }

            var MainData = form.getData();
            if (MainData.FRStockID.toString() == "") {
                mini.alert("设置错误:配送门店不能为空!");
                return false;
            }
            if (MainData.FStockID.toString() == "") {
                mini.alert("设置错误:收货门店不能为空!");
                return false;
            }
            return true;
        }

        function saveBill() {
            BillState(1);//先锁定
            if (checkValid() == false) {
                BillState(4);//先锁定
                return;
            }
            var MainData = form.getData();
            var cDate = mini.getbyName("FDate").getFormValue();
            var rCnt = grid.getData().length;
            var sBillNo = MainData.FBillNo;

            var mjson = '[{"KInterID":"' + MainData.KInterID + '",';
            mjson = mjson + '"FBillNo":"' + MainData.FBillNo + '",';
            mjson = mjson + '"FSystemAppID":"' + uID + '",';
            mjson = mjson + '"FSupplyID":"' + MainData.FRStockID + '",';
            mjson = mjson + '"FStockID":"' + MainData.FStockID + '",';
            mjson = mjson + '"FDate":"' + cDate + '",';
            mjson = mjson + '"FDataType":"' + datatype + '",';
            mjson = mjson + '"FRemark":"' + MainData.FRemark + '",';
            mjson = mjson + '"KInterID_Pra":"' + MainData.KInterID_Pra + '",';
            mjson = mjson + '"FBillNO_Pra":"",';
            mjson = mjson + '"KInterID_DawOut":"' + MainData.KInterID_DawOut + '",';
            var json = '"goods_list":[';
            for (var i = 0; i < rCnt; i++) {
                var rs = grid.getRow(i);
                debugger;
                if (typeof (rs.mFNumber) == "undefined") continue;
                js = "{"              
                js = js + '"mFItemID":' + rs.mFItemID + ",";
                js = js + '"mFConvert":1,';
                var qty = typeof (rs.mFQty) == "undefined" ? 0 : rs.mFQty;
                js = js + '"mFQty":' + qty + ",";
                var price = typeof (rs.mFPrice) == "undefined" ? 0 : rs.mFPrice;
                js = js + '"mFPrice":' + price + ",";
                var money = typeof (rs.mFAmount) == "undefined" ? 0 : rs.mFAmount;
                js = js + '"mFAmount":' + money + ",";
                var remark = typeof (rs.mFRemark) == "undefined" ? "" : rs.mFRemark;
                js = js + '"mFRemark":"' + remark + '",';
                js = js + '"mFEntryID_Pra":"' + rs.mFEntryID_Pra + '",';
                js = js + '"mFEntryID_DawOut":"' + rs.mFEntryID_DawOut + '",';
                js = js + '"mFInterID_Pra":"' + rs.mFInterID_Pra + '",';
                js = js + '"mFInterID_DawOut":"' + rs.mFInterID_DawOut + '",';
                js = js + '"mFQty_Pra":"' + rs.mFQty_Pra + '",';
                js = js + '"mFQty_DawOut":"' + rs.mFQty_DawOut + '",';
                var mFBillNO_Pra = typeof (rs.mFBillNO_Pra) == "undefined" ? "" : rs.mFBillNO_Pra;
                js = js + '"mFBillNO_Pra":"' + mFBillNO_Pra + '",';
                var mFBillNO_DawOut = typeof (rs.mFBillNO_DawOut) == "undefined" ? "" : rs.mFBillNO_DawOut;
                js = js + '"mFBillNO_DawOut":"' + mFBillNO_DawOut + '"';
                js = js + "}"
                json = json + js + ","

            }
            json = json.substring(0, json.length - 1);
            json = json + "]";
            if (json.length < 8) {
                mini.alert("没有要保存的数据！");
                return;
            }
            mjson = mjson + json + "}]";
            $.ajax({
                url: '../Bill/SaveBillData_DawIn',
                data: { mdatajson: mjson },
                type: "post",
                cache: false,
                success: function (txt) {
                    var ret = mini.decode(txt);
                    if (ret.id != -1) {
                        mini.showTips({ content: '保存成功', state: "success", x: "center", y: "top", timeout: 3000 });
                        mini.getbyName("KInterID").setValue(ret.id);
                        mini.getbyName("FBillNo").setValue(ret.FBillNo);
                        mini.getbyName("ScanAdd").setEnabled(false);
                        BillState(1);
                    }
                    else {
                        mini.alert("异常:" + ret.Status);
                    }
                },
                error: function (xhr) {
                    mini.alert("异常:" + xhr.responseText);
                }
            });
        }

        function onGetSourceBill() {
            mini.open({
                url: "../Bill/FindBill",
                title: "查看单据", width: 900, height: 530,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { BillKind: "1211" };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    if (action) {
                        if ((action.KInterID == null) || (action.KInterID == "undefined")) return;
                        mini.getbyName("KInterID_DawOut").setValue(action.KInterID);
                        mini.getbyName("KInterID_Pra").setValue(action.KInterID_Pra);
                        LoadDataFromOut();
                    }
                }
            });
        }

        function LoadDataFromOut() {
            var minterid = mini.getbyName("KInterID_DawOut").value;
           $.ajax({
                url: '../Bill/GetBillData',
                data: { mSystemAppID: uID, mBillID: minterid, mDataType: "1211" },
                dataType: 'text',
                async: false,
                success: function (text) {
                    var dataR = mini.decode(text);
                    grid.setData(dataR.datadetail);
                    if (dataR.datamaster.length > 0) {
                        var mdata = dataR.datamaster[0];
                        form.setData(mdata);
                        mini.getbyName("FBillNO_Dawout").setValue(mdata.KInterID);
                        mini.getbyName("FBillNO_Dawout").setText(mdata.FBillNo);
                        mini.getbyName("FStockID").setValue(mdata.FStockID);
                        mini.getbyName("FStockID").setText(mdata.FStockNumber);
                        mini.getbyName("FStockName").setValue(mdata.FStockName);
                        mini.getbyName("FRStockID").setValue(mdata.FRStockID);
                        mini.getbyName("FRStockID").setText(mdata.FRStockNumber);
                        mini.getbyName("FRStockName").setValue(mdata.FRStockName);
                        mini.getbyName("FBillNo").setValue("");
                        mini.getbyName("KInterID").setValue("");
                        var date = new Date();
                        mini.getbyName("FDate").setValue(date);
                        BillState(4);
                    }
                }
            });
        }

        function ExcelImport() {
            var FSystemAppID = parent.window.getSystemAppID();
            mini.open({
                url: "../Bill/ImportData?mDataType=11&mSystemAppID=" + FSystemAppID + "&FStockID=" + mini.getbyName("FStockID").getValue(),
                title: "Excel导入", width: 450, height: 200,
                ondestroy: function (action) {
                    if (action == "close")
                        return;
                    if (action) {
                        if (action.length > 0) {
                            if (grid.getData().length > 0) {
                                if (grid.getData()[grid.getData().length - 1].mFNumber == undefined) {
                                    var row = grid.getRow(grid.getData().length - 1);
                                    grid.removeRow(row, false);
                                }
                            }
                            for (var i = 0, l = action.length; i < l; i++) {
                                var rw = action[i];
                                var newRow = {
                                    mFItemID: rw.mFItemID, mFNumber: rw.mFNumber, mFName: rw.mFName, mFModel: rw.mFModel,
                                    mFUnitName: rw.mFUnitName, mFQty: rw.mFQty, mFPrice: rw.mFPrice, mFCode: rw.mFCode,
                                    mFStockQty: rw.FStockQty, mFPosPrice: rw.mFPrice, mFPurPrice: rw.mFPrice, mFQty_Pra: 0,
                                    mFQty_DawOut: 0, mFEntryID_DawOut: 0, mFInterID_Pra: 0, mFEntryID_Pra: 0, mFInterID_DawOut: 0,
                                    mFBillNO_Pra: '', mFBillNO_DawOut: '', mFAmount: rw.mFAmount
                                };
                                grid.addRow(newRow, grid.getData().length);
                            }
                            BillState(4);
                            mini.get("ScanAdd").setEnabled(true);
                        }
                    }
                }
            });
        }

    </script>

</body>